<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Channel extends Common
{

	/**
	 * 查询数据
	 *
	 * @return
	 */
	public function read(Request $request) {
		if($this->hasPermission($request->sessInfo, 'channel:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );

		$name = $request->param('name', '', 'trim');
		if($name) {
			array_push( $where, ['name', 'like',  "%{$name}%" ] );
		}

		$query = Db::name('cfg_channel')->where($where)->order($sort, $order)->paginate($limit);

		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $query->items()  ];
	}

	/**
	 * 更新数据
	 *
	 * @return
	 */
	public function update(Request $request) {
		if($this->hasPermission($request->sessInfo, 'channel:update')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$param = $request->param();
		if( isset($param['state']) ) {
			$status = $request->param('status', '', 'trim');
			Db::name('cfg_channel')->where('id', $param['state'])
					->where('companyId', $request->sessInfo['companyId'])
					->update([ 'status' => $status]);
			return ['code'=>0, 'msg'=>'操作成功'];
		}

		$remark = $request->param('remark', '', 'trim');
		$status = $request->param('status', '', 'trim');
		$name = $request->param('name', '', 'trim');
		$id = $request->param('id', '', 'trim');
		Db::name('cfg_channel')->where('id', $id)->where('companyId', $request->sessInfo['companyId'])
				->update([  'name' => $name, 'remark' => $remark, 'status' => $status]);
		return ['code'=>0, 'msg'=>'操作成功'];
	}

	/**
	 * 删除数据
	 *
	 * @return
	 */
	public function delete(Request $request) {
		if($this->hasPermission($request->sessInfo, 'channel:delete')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$beginTime = microtime(true);
		$param = $request->param();
		if( !isset($param['id']) ) {
			return ['code'=> 403, 'msg'=> '操作失败!'];
		}
		$delArray = [];
		$idArray = explode(',', $param['id']);
		if(count($idArray) > 0) {
			foreach( $idArray as $value ) {
				array_push($delArray, Db::table('cfg_channel')->where('id', $value)->where('companyId', $request->sessInfo['companyId'])->value('name'));
				Db::table('cfg_channel')->where('companyId', $request->sessInfo['companyId'])->where('id', $value)->delete();
			}
			foreach( $delArray as $value ) {
				if( $value ) {
					$endTime = microtime(true);
					$this->cfg_write_oper($request, $request->sessInfo['token'], $request->sessInfo['companyId'], $request->sessInfo['nickname'],
					$request->sessInfo['username'], round($endTime - $beginTime, 3), '账户负责人', '删除负责人['.$value.']', 0, json_encode($param));
				}
			}
		}
		return ['code'=> 0, 'msg'=> '删除成功!'];
	}

	/**
	 * 查询分组是否已存在
	 *
	 * @return
	 */
	public function query(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'channel:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$param = $request->param('param', '', 'trim');
		if(empty($param)){
			return json(['code'=>0, 'data' => [], 'msg'=>'']);
		}
		$query = Db::table('cfg_channel')->where('companyId', $request->sessInfo['companyId'])->where('name', $param)->value('name');
		if(empty($query)){
			return json(['code'=>0, 'data' => [], 'msg'=>'']);
		}
		return json(['code'=>0, 'data' => [['name' => $query]], 'msg'=>'']);
	}

	/**
	 * 创建数据
	 *
	 * @return
	 */
	public function create(Request $request) {
		if($this->hasPermission($request->sessInfo, 'channel:save')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$platform = $request->param('platform', '', 'trim');
		$bazaarId = $request->param('bazaarId', '', 'trim');
		$remark = $request->param('remark', '', 'trim');
		$status = $request->param('status', '', 'trim');
		$name = $request->param('name', '', 'trim');
		$query = Db::table('cfg_channel')->where('companyId', $request->sessInfo['companyId'])->where('name', $name)->value('name');
		if(!empty($query)) {
			return ['code'=>403, 'msg'=>'操作失败, 开户渠道已存在!'];
		}
		Db::name('cfg_channel')->insert([ 'companyId' => $request->sessInfo['companyId'], 'name' => $name, 'remark' => $remark, 'status' => $status, 'datatime' => date('Y-m-d H:i:s')]);
		return ['code'=>0, 'msg'=>'操作成功'];
	}

}
